package com.fate.securecheckplatform.ui.frags;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.fate.securecheckplatform.R;
import com.fate.securecheckplatform.base.BaseFragment;
import com.fate.securecheckplatform.bean.AppVersionEntity;
import com.fate.securecheckplatform.bean.TableForbiddenPerformance;
import com.fate.securecheckplatform.bean.TableForbiddenThing;
import com.fate.securecheckplatform.db.DataBase;
import com.fate.securecheckplatform.net.MyObserver;
import com.fate.securecheckplatform.net.RequestUtils;
import com.fate.securecheckplatform.utils.DownLoadApk;
import com.fate.securecheckplatform.utils.ExecutorManager;
import com.fate.securecheckplatform.utils.LogUtil;
import com.fate.securecheckplatform.utils.ToastUtil;
import com.fate.securecheckplatform.utils.Util;
import com.trello.rxlifecycle3.components.support.RxAppCompatActivity;

import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Author:   RandBII
 * DATE:   2020/1/8
 * Description: 设置
 */
public class SetFragment extends BaseFragment {

    private static final String TAG = "RegisterFragment";
    @BindView(R.id.tv_version)
    TextView mTvVersion;
    @BindView(R.id.et_new_password)
    EditText mEtNewPassowrd;
    @BindView(R.id.et_old_password)
    EditText mEtOldPassword;
    @BindView(R.id.et_confirm_password)
    EditText mEtConfirmPassword;
    @BindView(R.id.btn_up_data)
    Button mBtnUpLoad;
    private DataBase dataBase;


    public static SetFragment get() {
        SetFragment fragment = new SetFragment();
        Bundle bundle = new Bundle();
        bundle.putString("name", TAG);
        fragment.setArguments(bundle);
        return fragment;
    }

    private void ObtainFragment() {

    }

    @Override
    protected void initData() {

    }

    @SuppressLint("SetTextI18n")
    @Override
    protected void initView(View root) {
        dataBase = DataBase.getInstance(getActivity());
        String versionCode = Util.getAppVersion(getActivity());
        mTvVersion.setText("当前版本:" + versionCode);
    }

    @Override
    protected int layoutResourceId() {
        return R.layout.fragment_set;
    }

    @OnClick({R.id.btn_save, R.id.btn_check_version, R.id.btn_up_data})
    public void onClickEvent(View v) {
        switch (v.getId()) {
            case R.id.btn_save:
                save();
                break;
            case R.id.btn_check_version:
                checkVersion();
                break;
            case R.id.btn_up_data:
                uploadData();
                break;
        }
    }

    /**
     * 上传数据
     */
    private void uploadData() {

        Future<List<TableForbiddenPerformance>> future = ExecutorManager.getService().submit(() -> dataBase.forbiddenObjectDao().getForbiddenPerformances());
        List<TableForbiddenPerformance> performances = null;
        try {
            performances = future.get();
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if (performances == null || performances.size() == 0) {
            ToastUtil.toast("暂无数据");
            return;
        }

        for (TableForbiddenPerformance performance : performances) {
            performance.setQmz64(Util.imageToBase64(performance.getLkqmt()));
            List<TableForbiddenThing> things = performance.getThins();
            if (things != null && things.size() > 0) {
                for (TableForbiddenThing t : things) {
                    t.setWjpt64(Util.imageToBase64(t.getWjpt()));
                }
            }
        }

        RequestUtils.upLoadData((RxAppCompatActivity) getActivity(), performances, new MyObserver<String>(getActivity()) {
            @Override
            public void onSuccess(String result) {
                ToastUtil.toast("" + result);
            }

            @Override
            public void onFailure(Throwable e, String errorMsg) {
                LogUtil.e("-->", "" + errorMsg);
            }
        });
    }

    /**
     * 检测版本更新
     */
    private void checkVersion() {
        RequestUtils.getVersionInfo((RxAppCompatActivity) getActivity(),
                Util.getAppVersion(getActivity()),
                new MyObserver<AppVersionEntity>(getActivity()) {
                    @Override
                    public void onSuccess(AppVersionEntity result) {
                        if (result != null) {
                            String apkUrl = result.getApk_file_url();
                            if (!TextUtils.isEmpty(apkUrl)) {
                                ToastUtil.toast("正在下载，请耐心等待");
                                DownLoadApk.getInstant().downloadAPK(apkUrl, getActivity());
                            } else {
                                ToastUtil.toast("已是最新版本");
                            }
                        }
                    }

                    @Override
                    public void onFailure(Throwable e, String errorMsg) {
                        ToastUtil.toast("" + errorMsg);
                        LogUtil.e("-->", "-->" + errorMsg);
                    }
                });
    }

    private void save() {

    }
}
